< previous page page_467 next page >

Page 467
Trapping Events for the Data Grid Control
The data grid control has several unique events that you can use to monitor user actions in your grid. The following events can be used to check the contents of your data table before you allow the user to continue:
·BeforeInsert: This event occurs before a new row is inserted into the grid. Use this event to confirm that the user wants to add a new record.
·AfterInsert: This event occurs right after a new row has been inserted into the grid. Use this event to perform clean-up chores after a new record has been added.
·BeforeUpdate: This event occurs before the data grid writes the changes to the data control. Use this event to perform data validation at the record level.
·AfterUpdate: This event occurs after the changed data has been written to the data control. Use this event to perform miscellaneous chores after the grid has been updated.
·BeforeDelete: This event occurs before the selected record(s) are deleted from the grid. Use this event to perform confirmation chores before deleting data.
·AfterDelete: This event occurs after the user has already deleted the data from the grid. Use this event to perform related chores once the grid has been updated.
You can use the events listed here to perform field and record-level validation and force user confirmation on critical events, such as adding a new record or deleting an existing record. Let's add some code to the DBGRID.VBP project to illustrate the use of these events.
The Add Record Events
First, add code that monitors the adding of new records to the grid. Select the grid control and open the DBGrid1_BeforeInsert event. Add the code in Listing 12.1.
Type.gifListing 12.1. Code to monitor addition of new records to a data-bound grid.
Private Sub DBGrid1_BeforeInsert(Cancel As Integer)
    '
    ' make user confirm add operation
    '
    Dim nResult As Integer
    '
    nResult = MsgBox("Do you want to add a new record?",
    _vbInformation + vbYesNo, "DBGrid.BeforeInsert")
    If nResult = vbNo Then
        Cancel = True   ' cancel add
    End If
End Sub

 
< previous page page_467 next page >

If you like this book, buy it!